<?php
class Merchant_Model_Orders
{

	private $_table;
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_Orders();
	}


	public function add($row){
		return $this->_table->insert($row);
	}

	public  function update($row,$value,$field='order_id'){
		$row['last_modified_time'] = date("Y-m-d H:i:s");
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);
		return $this->_table->update($row, $where);

	}


	public function delete($value,$field='order_id'){

		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?", $value);

		return $this->_table->delete($where);

	}


	public function getByValue($value,$field='order_id'){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}

	public  function getAdapter(){
		return $this->_table->getAdapter();
	}
    public function getByCondition($condition,$type='*',$orderby="",$page=0,$pageSize=0){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
        if(isset($condition["orders_code"]) && $condition["orders_code"]!=''){
            $select->where("orders_code =?",$condition["orders_code"]);
        }
		if(isset($condition["customer_order_code"]) && $condition["customer_order_code"]!=''){
            $select->where("customer_order_code =?",$condition["customer_order_code"]);
        }
		if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
            $select->where("customer_id = ?",$condition["customer_id"]);
        }
		
		if(isset($condition["shipping_method"]) && $condition["shipping_method"]!=''){
            $select->where("shipping_method = ?",$condition["shipping_method"]);
        }
		
        if(isset($condition["warehouse_id"]) && $condition["warehouse_id"]!=''){
            $select->where(" warehouse_id =?",$condition["warehouse_id"]);
        }
        if(isset($condition["consignee_first_name"]) && $condition["consignee_first_name"]!=''){
            $select->where(" consignee_first_name =?",$condition["consignee_first_name"]);
        }
        if(isset($condition["orders_status"]) && $condition["orders_status"]!==''){
            $select->where(" orders_status =?",$condition["orders_status"]);
        }

        if(isset($condition["underreview_status"]) && !empty($condition["underreview_status"])){
            $select->where(" underreview_status in(?)",$condition["underreview_status"]);
            //echo $select->__toString();exit;
        }

        if(isset($condition["add_date"]) && $condition["add_date"]!=''){
            $select->where(" TO_DAYS(add_time) =TO_DAYS(?) ",($condition["add_date"]));
        }
		if(isset($condition["add_time_start"]) && $condition["add_time_start"]!=''){
            $select->where(" add_time>=? ",($condition["add_time_start"]));
        }
		if(isset($condition["add_time_end"]) && $condition["add_time_end"]!=''){
            $select->where(" add_time<=? ",($condition["add_time_end"]));
        }
		
		
		if(isset($condition["submit_time_start"]) && $condition["add_date_start"]!=''){
            $select->where(" add_time>=? ",($condition["add_date_start"]));
        }
		if(isset($condition["submit_time_end"]) && $condition["submit_time_end"]!=''){
            $select->where(" submit_time<=? ",($condition["submit_time_end"]));
        }
		
		
		
		if(isset($condition["ship_date_start"]) && $condition["ship_date_start"]!=''){
            $select->where(" ship_date>=? ",($condition["ship_date_start"]));
        }
		if(isset($condition["ship_date_end"]) && $condition["ship_date_end"]!=''){
            $select->where(" ship_date<=? ",($condition["ship_date_end"]));
        }
		if(isset($condition["consignee_country_id"]) && $condition["consignee_country_id"]!=''){
			 $select->where(" consignee_country_id=? ",($condition["consignee_country_id"]));
		}

        if(isset($condition["order_id"]) && $condition["order_id"]!=''){
            $select->where(" order_id <>?",$condition["order_id"]);
        }
        if(isset($condition["order_status_arr"]) && $condition["order_status_arr"]!=''){
            $select->where(" orders_status in (?)",$condition["order_status_arr"]);
        }
        if(isset($condition["order_status_arr_not"]) && $condition["order_status_arr_not"]!=''){
        	$select->where(" orders_status not in (?)",$condition["order_status_arr_not"]);
        }
        
		if(isset($condition["orderId"]) && $condition["orderId"]!=''){
            $select->where(" order_id =?",$condition["orderId"]);
        }
//        echo $select;exit;
        if('count(*)'==$type){            
            return $this->_table->getAdapter()->fetchone($select);
        }else{
            if($orderby!='')
            {
                $orderbyArr= explode(',',$orderby);
                foreach($orderbyArr as $order)
                {
                    $select->order($order);
                }

            }
            if($pageSize>0 && $page>0)
            {
                $start = ($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
//            echo $select->__toString();exit;
            $rows = $this->_table->getAdapter()->fetchAll($select);
		    return $rows;
        }
	}
	
	/**
	* 根据产品查询未发货的订单
	*
	*/
	public function getByProductId($productId){
		$select = $this->_table->getAdapter()->quoteInto("select count(*) from orders,order_product where orders.orders_code = order_product.orders_code and orders.orders_status in(1,2,3,4) and order_product.product_id = ?",$productId);
		return $this->_table->getAdapter()->fetchOne($select);
	}

}